using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class SensorSwitch:Switch
    {
        
        protected bool _beenTouch = false;
       // Body _touchBody=null;

        List<Body> _contactBodys = new List<Body>();

        public SensorSwitch(World world, Stage stage)
            : base(world, stage)
        {
            MusicOn = true;
            _switchOn = new Animation("images/items/sensorButton", new Rectangle(2, 34, 50, 30), new Point(50, 30));
            _switchOff = new Animation("images/items/sensorButton", new Rectangle(2, 2, 50, 30), new Point(50, 30));
            CurrentAnimation = _switchOff;
            _isActive = false;


        }

        public bool MusicOn { get; set; }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.BeginContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player" || contactBody.GetUserData() == "monster"
                || contactBody.GetUserData() == "ElementPiece")
            {
                if (!_contactBodys.Contains(contactBody))
                    _contactBodys.Add(contactBody);
              
                if (!_isActive && _contactBodys.Count() == 1)
                {
                    if(MusicOn)MusicManager.PlayMusic("sensor");
                    foreach (ChangeStateable _s in _targetList)
                    {
                        _isActive = true;
                        _s.ChangeState(true);
                        CurrentAnimation = _switchOn;
                    }
                }
            } 
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
           

            if (_contactBodys.Contains(contactBody))
                _contactBodys.Remove(contactBody);

            if (_isActive && _contactBodys.Count()==0)
                foreach (ChangeStateable _s in _targetList)
                {
                    _isActive = false;
                    _s.ChangeState(false);
                    CurrentAnimation = _switchOff;
                }
        }

        public override void Restart()
        {
            base.Restart();
            //CurrentAnimation = _switchOff;
        }
    }
}
